package com.leo.foodmaster.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.SysDictType;
import com.leo.foodmaster.admin.system.service.ISysDictTypeService;
import com.leo.foodmaster.commons.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-06-29
 */
@RestController
@AllArgsConstructor
@Api(tags = "字典类型管理")
@RequestMapping("/system/dict/type")
public class SysDictTypeController {
    private final ISysDictTypeService sysDictTypeService;

    @GetMapping("/list")
    @ApiOperation("分页条件查询字典类型列表")
    public Result<IPage<SysDictType>> getDictTypePageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "字典类型名称") @RequestParam(required = false) String name,
            @ApiParam(value = "字典类型状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime ) {
        IPage<SysDictType> pageList  = sysDictTypeService.getDictTypePageList(new Page<>(pageNum, pageSize), name,
                status, beginTime, endTime);
        return Result.success(pageList);
    }

    @GetMapping("/listAll")
    @ApiOperation("查询全部字典类型列表")
    public Result<List<SysDictType>> getDictTypeList() {
        List<SysDictType> list  = sysDictTypeService.list();
        return Result.success(list);
    }

    @GetMapping("/{dictTypeId}")
    @ApiOperation("查询字典类型详情")
    public Result<SysDictType> getDictTypeById(@ApiParam("字典类型Id") @PathVariable Long dictTypeId) {
        SysDictType dictType  = sysDictTypeService.getById(dictTypeId);
        return Result.success(dictType);
    }

    @PostMapping
    @ApiOperation("新增字典类型")
    public Result<Boolean> addSysDictType(@RequestBody SysDictType dictType) {
        boolean save = sysDictTypeService.save(dictType);
        return Result.success(save);
    }

    @PutMapping
    @ApiOperation("修改字典类型")
    public Result<Boolean> updateSysDictType(@RequestBody SysDictType dictType) {
        boolean update = sysDictTypeService.updateById(dictType);
        // 刷新字典数据缓存
        if (update) {
            sysDictTypeService.listAllDict();
        }
        return Result.success(update);
    }

    @DeleteMapping("/{dictTypeIds}")
    @ApiOperation("删除字典类型")
    public Result<Object> deleteSysDictType(@ApiParam("字典类型ids") @PathVariable String dictTypeIds) {
        return sysDictTypeService.deleteSysDictType(dictTypeIds);
    }


}

